package com.example.contentproviderdemo;

import android.content.ContentProvider;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.UriMatcher;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.net.Uri;
import android.util.Log;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

public class MyContentProvider extends ContentProvider {

    private static final String TAG = "MyContentProvider";

    //如果不匹配采用UriMatcher.NO_MATCH(-1)
    private static final UriMatcher MATCHER = new UriMatcher(UriMatcher.NO_MATCH);

    private static final int CODE_NOMATCHER = 1;   //无参
    private static final int CODE_PARAM = 0;     //有参

    static {
        //对等待匹配的URI进行匹配操作，必须符合com.test.providers.SimpleContentProvider/artical格式
        //匹配返回CODE_NOPARAM，不匹配返回-1
        MATCHER.addURI("com.test.provider.MyContentProvider","user",CODE_NOMATCHER);

        //#表示数字 com.test.providers.SimpleContentProvider/artical/10
        //匹配返回CODE_PARAM，不匹配返回-1
        MATCHER.addURI("com.test.provider.MyContentProvider","user/#",CODE_PARAM);
    }

    private DbHelper dbHelper;
    private SQLiteDatabase db;


    @Override
    public boolean onCreate() {
        dbHelper = new DbHelper(getContext());
        db = dbHelper.getWritableDatabase();
        // 初始化两个表的数据(先清空两个表,再各加入一个记录)
        db.execSQL("delete from user");
        db.execSQL("insert into user values(1,'Carson');");
        db.execSQL("insert into user values(2,'Kobe');");
        db.execSQL("delete from job");
        db.execSQL("insert into job values(1,'Android');");
        db.execSQL("insert into job values(2,'iOS');");
        return true;
    }

    @Nullable
    @Override
    public Cursor query(@NonNull Uri uri, @Nullable String[] projection, @Nullable String selection, @Nullable String[] selectionArgs, @Nullable String sortOrder) {
        Log.e(TAG, "query: " + "查询" );
       switch (MATCHER.match(uri)){
           case CODE_NOMATCHER:
               return db.query("user",projection,selection,selectionArgs,null,null,sortOrder);
           case CODE_PARAM:
               long id = ContentUris.parseId(uri);
               String where = "_id = " + id;
               if(null != selection && !(selection.trim()).equals("")){
                   where += "and " + selection;
               }
               return db.query("user", projection, where, selectionArgs, null, null, sortOrder);
           default:
               throw new IllegalArgumentException("This is unknow uri: " + uri);
       }
    }

    /**
     * 返回对应的内容类型
     * 如果返回集合的内容类型，必须以com.test.android.cursor.dir开头
     * 如果是单个元素，必须以com.test.android.cursor.item开头
     */
    @Nullable
    @Override
    public String getType(@NonNull Uri uri) {

        switch (MATCHER.match(uri)){
            case CODE_NOMATCHER:
                return "com.test.android.cursor.dir/user";
            case CODE_PARAM:
                return "com.test.android.cursor.item/user";
            default:
                throw new IllegalArgumentException("This is unknow uri: " + uri);
        }
    }

    @Nullable
    @Override
    public Uri insert(@NonNull Uri uri, @Nullable ContentValues values) {

        switch (MATCHER.match(uri)){
            case CODE_NOMATCHER:
                long id = db.insert("user",null,values);
                Uri insertUri = ContentUris.withAppendedId(uri,id);
                return insertUri;
            default:
                throw new IllegalArgumentException("This is unknow uri: " + uri);
        }
    }

    @Override
    public int delete(@NonNull Uri uri, @Nullable String selection, @Nullable String[] selectionArgs) {

        switch (MATCHER.match(uri)){
            case CODE_NOMATCHER:
                return db.delete("user",selection,selectionArgs);
            case CODE_PARAM:
                long id = ContentUris.parseId(uri);        //取得Uri后面的数字
                String where = "_id = " + id;
                if(null != selection && !(selection.trim()).equals("")){
                    where += "and " + selection;
                }
                return db.delete("user", where, selectionArgs);
            default:
                throw new IllegalArgumentException("This is unknow uri: " + uri);
        }
    }

    @Override
    public int update(@NonNull Uri uri, @Nullable ContentValues values, @Nullable String selection, @Nullable String[] selectionArgs) {

        switch (MATCHER.match(uri)) {
            case CODE_NOMATCHER:
                return db.update("user", values, selection, selectionArgs);    //更新所有记录
            case CODE_PARAM:
                long id = ContentUris.parseId(uri);        //取得Uri后面的数字
                String where = "_id = " + id;
                if(null != selection && !(selection.trim()).equals("")){
                    where += " and " + selection;
                }
                return db.update("user", values, where, selectionArgs);
            default:
                throw new IllegalArgumentException("This is unknow uri: " + uri);
        }
    }
}
